package com.mojool.game.citytycoon;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.ZoomCamera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXProperties;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTile;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTileProperty;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLoader.ITMXTilePropertiesListener;
import org.anddev.andengine.entity.layer.tiled.tmx.util.exception.TMXLoadException;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.Debug;

import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.widget.Toast;

public class CityTycoon extends BaseGameActivity implements IOnSceneTouchListener {

	@Override
	public Engine onLoadEngine() {
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int CAMERA_WIDTH = metrics.widthPixels;
		int CAMERA_HEIGHT = metrics.heightPixels;

		/*
		this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), mBoundChaseCamera));
		 */
		this.mCamera = new ZoomCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera));
	}

	private Texture mTexture;
	private TextureRegion mZoomInTextureRegion;
	
	@Override
	public void onLoadResources() {
		this.mTexture = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mZoomInTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "gfx/zoomin.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(this.mTexture);
	}

	//private BoundCamera mBoundChaseCamera;
	private ZoomCamera mCamera;
	private TMXTiledMap mTMXTiledMap;

	@Override
	public Scene onLoadScene() {
		// TODO Auto-generated method stub
		final Scene scene = new Scene(1);
		scene.setOnAreaTouchTraversalFrontToBack();
		scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

		try {
			final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, new ITMXTilePropertiesListener() {
				@Override
				public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap, final TMXLayer pTMXLayer, final TMXTile pTMXTile, final TMXProperties<TMXTileProperty> pTMXTileProperties) {
					/*
					if(pTMXTileProperties.containsTMXProperty("cactus", "true")) {
						MojoolCity.this.mCactusCount++;
					}
					 */
				}
			});
			this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "tmx/map.tmx");
		} catch (final TMXLoadException tmxlex) {
			Debug.e(tmxlex);
		}

		final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
		scene.getBottomLayer().addEntity(tmxLayer);

		//this.mBoundChaseCamera.setBounds(0, tmxLayer.getWidth(), 0, tmxLayer.getHeight());
		//this.mBoundChaseCamera.setBoundsEnabled(true);		

		scene.setOnSceneTouchListener(this);
		scene.setTouchAreaBindingEnabled(true);

		return scene;
	}

	@Override
	public void onLoadComplete() {
	}

	private float mTouchX;
	private float mTouchY;

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
			//Toast.makeText(this, "ACTION_DOWN", Toast.LENGTH_SHORT).show();
			mTouchX = pSceneTouchEvent.getMotionEvent().getX();
			mTouchY = pSceneTouchEvent.getMotionEvent().getY();
			
			final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
			final TMXTile tmxTile = tmxLayer.getTMXTileAt(mTouchX, mTouchY);
			if (tmxTile != null) {
				//String text = new Integer(tmxTile.getTileColumn()).toString() + ":" + new Integer(tmxTile.getTileRow()).toString();
				//Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
			}
			
			//this.mCamera.setZoomFactor(3.0f);
		} else if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_MOVE) {
			//this.mCamera.setZoomFactor(1.0f);

			float newX = pSceneTouchEvent.getMotionEvent().getX();
			float newY = pSceneTouchEvent.getMotionEvent().getY();

			float mTouchOffsetX = (newX - mTouchX);
			float mTouchOffsetY = (newY - mTouchY);

			float newScrollX = this.mCamera.getCenterX() - mTouchOffsetX;
			float newScrollY = this.mCamera.getCenterY() - mTouchOffsetY;

			this.mCamera.setCenter(newScrollX, newScrollY);

			mTouchX = newX;
			mTouchY = newY;
			//Toast.makeText(this, "Move", Toast.LENGTH_SHORT).show();
		}
		return true;
	}

}